# -*- coding: utf-8 -*-
import oss2
import time
import os

"""
weihua 250208
上线时删除下描述的明文key, 换成注释掉的形式，使用时需要先设置 key 的环境变量: 
    export OSS_ACCESS_KEY_ID='你的AccessKeyId'
    export OSS_ACCESS_KEY_SECRET='你的AccessKeySecret'
"""
# from oss2.credentials import EnvironmentVariableCredentialsProvider
# # 从环境变量中获取访问凭证。运行本代码示例之前，请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
# auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

def upload_file_to_oss(local_file_path='SDxeon/backend/pic_model/uml-test.jpg'):
    """
    上传文件到云oss
    args:
        local_file_path: 本地文件路径
    returns:
        oss_url: 文件在OSS中的访问URL
    """
    _, file_extension = os.path.splitext(local_file_path)
    timestamp = str(int(time.time())) 
    # 文件名
    file_name = f"pic-model/{timestamp}{file_extension}"

    OSS_ACCESS_KEY_ID = "LTAI5tMqwjwPegAc7nwZibEa"
    OSS_ACCESS_KEY_SECRET = "GelxWAr0ZmbgiOP353oLEUlaNdM2le"
    auth = oss2.Auth(OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET)

    endpoint = "https://oss-cn-beijing.aliyuncs.com/"
    region = "cn-beijing"
    bucketName = "opea-smartdrawer-weihua"
    bucket = oss2.Bucket(auth, endpoint, bucketName, region=region)
    # 上传文件
    bucket.put_object_from_file(file_name, local_file_path)
    """
    oss 中文件访问路径？
    https://opea-smartdrawer-weihua.oss-cn-beijing.aliyuncs.com/pic-model/audrey_hepburn.jpg
    """
    oss_url = f"https://opea-smartdrawer-weihua.oss-cn-beijing.aliyuncs.com/{file_name}"
    print(f"OSS 存储访问路径: {oss_url}")
    return oss_url

if __name__ == "__main__":
    local_file_path = 'SDxeon/backend/pic_model/audrey_hepburn.jpg'
    upload_file_to_oss(local_file_path)
